using System;
using Consts;
using Dto;
using Model;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class MyStatePanel : StatePanel
    {
        private Button _btnDeal;
        private Button _btnNDeal;
        private Button _btnGrad;
        private Button _btnNGrad;
        private Button _btnReady;

        private ReqBase _reqBase;


        protected override void Awake()
        {
            base.Awake();
            Bind(UIEvent.SHOW_GRAB_BUTTON, UIEvent.SHOW_DEAL_BUTTON, UIEvent.SET_MY_PLAYER_DATA,
                UIEvent.PLAYER_HIDE_READY_BUTTON);
        }

        public override void Execute(int eventCode, object message)
        {
            base.Execute(eventCode, message);
            switch (eventCode)
            {
                case UIEvent.SHOW_GRAB_BUTTON:
                {
                    bool active = (bool) message;
                    _btnGrad.gameObject.SetActive(active);
                    _btnNGrad.gameObject.SetActive(active);
                    break;
                }
                case UIEvent.SHOW_DEAL_BUTTON:
                {
                    bool active = (bool) message;

                    _btnDeal.gameObject.SetActive(active);
                    _btnNDeal.gameObject.SetActive(active);
                    break;
                }
                case UIEvent.SET_MY_PLAYER_DATA:
                {
                    this.user = message as Account;
                    break;
                }
                case UIEvent.PLAYER_HIDE_READY_BUTTON:
                {
                    _btnReady.gameObject.SetActive(false);
                    break;
                }
                default:
                    break;
            }
        }

        protected override void Start()
        {
            base.Start();
            _btnDeal = transform.Find("BtnDeal").GetComponent<Button>();
            _btnNDeal = transform.Find("BtnNDeal").GetComponent<Button>();
            _btnGrad = transform.Find("BtnGrad").GetComponent<Button>();
            _btnNGrad = transform.Find("BtnNGrad").GetComponent<Button>();
            _btnReady = transform.Find("BtnReady").GetComponent<Button>();

            _btnGrad.gameObject.SetActive(false);
            _btnNGrad.gameObject.SetActive(false);


            _btnGrad.onClick.AddListener(
                delegate { GrabClick(true); });
            _btnNGrad.onClick.AddListener(
                delegate { GrabClick(false); });
            _btnReady.onClick.AddListener(ReadyClick);

            _btnGrad.gameObject.SetActive(false);
            _btnNGrad.gameObject.SetActive(false);
            _btnDeal.gameObject.SetActive(false);
            _btnNDeal.gameObject.SetActive(false);

            _reqBase = new ReqBase(SocketConst.MATCH);

            Account account = Models.GameModel.MatchRoom.uids[Models.GameModel.Account.id];
            this.user = account;
            
                
        }

        private void DealClick()
        {
            // 向服务器发送出牌
        }

        private void NDealClick()
        {
            // 发送不出牌
        }

        private void GrabClick(bool result)
        {
            if (result)
            {
                // 抢地主
            }
            else
            {
                // 不抢地主
            }
        }

        private void ReadyClick()
        {
            // 向服务器发送准备
            _reqBase.Change(MatchCodeConst.READY, null);
            Dispatch(AreaCode.NET, 0, _reqBase);
        }

        protected override void ReadyState()
        {
            base.ReadyState();
            _btnReady.gameObject.SetActive(false);
        }
    }
}